//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Policy Injection Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Transactions;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.Design;
using EntLibContrib.PolicyInjection.CallHandlers.Configuration.Design.Properties;

namespace EntLibContrib.PolicyInjection.CallHandlers.Configuration.Design
{
	/// <summary>
	/// A <see cref="ConfigurationNode"/> that represents the configuration
	/// of an <see cref="TransactionScopeCallHandler" /> in the Configuration
	/// Console.
	/// </summary>
	public class TransactionScopeCallHandlerNode : CallHandlerNode
	{
		#region Fields
		private TransactionScopeOption transactionScopeOption;
		private TimeSpan timeout = TransactionManager.DefaultTimeout;
		private IsolationLevel isolationLevel;
		private EnterpriseServicesInteropOption interopOption;
		private bool complete = true;
		#endregion

		#region Properties
		/// <summary>
		/// Gets or Sets the Transaction Scope Option
		/// </summary>
		[SRDescription("TransactionScopeOptionDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public TransactionScopeOption TransactionScopeOption
		{
			get { return transactionScopeOption; }
			set { transactionScopeOption = value; }
		}

		/// <summary>
		/// Gets or Sets the Timeout
		/// </summary>
		[SRDescription("TimeoutDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		[TypeConverter(typeof(TimeSpanConverter))]
		public TimeSpan Timeout
		{
			get { return timeout; }
			set { timeout = value; }
		}

		/// <summary>
		/// Gets or Sets the Isolation Level
		/// </summary>
		[SRDescription("IsolationLevelDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public IsolationLevel IsolationLevel
		{
			get { return isolationLevel; }
			set { isolationLevel = value; }
		}

		/// <summary>
		/// Gets or Sets the Timeout
		/// </summary>
		[SRDescription("InteropOptionDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public EnterpriseServicesInteropOption InteropOption
		{
			get { return interopOption; }
			set { interopOption = value; }
		}

		/// <summary>
		/// Gets or Sets whether or not the Transaction will Complete on success.
		/// </summary>
		[SRDescription("CompleteDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public bool Complete
		{
			get { return complete; }
			set { complete = value; }
		}
		#endregion

		#region Construction
		/// <summary>
		/// Create a new <see cref="TransactionScopeCallHandlerNode"/> with default settings.
		/// </summary>
		public TransactionScopeCallHandlerNode()
			: this(new TransactionScopeCallHandlerData(Resources.TransactionScopeCallHandlerNodeName))
		{
		}

		/// <summary>
		/// Create a new <see cref="TransactionScopeCallHandlerNode"/> with the given settings.
		/// </summary>
		/// <param name="callHandlerData">Configuration information to initialize the node with.</param>
		public TransactionScopeCallHandlerNode(TransactionScopeCallHandlerData callHandlerData)
			: base(callHandlerData)
		{
			transactionScopeOption = callHandlerData.TransactionScopeOption;
			timeout = callHandlerData.Timeout;
			isolationLevel = callHandlerData.IsolationLevel;
			interopOption = callHandlerData.InteropOption;
			complete = callHandlerData.Complete;
		}
		#endregion

		#region Public Fields
		/// <summary>
		/// Converts the information stored in the node and generate
		/// the corresponding configuration element to store in
		/// an <see cref="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.IConfigurationSource"/>.
		/// </summary>
		/// <returns>
		/// Newly created <see cref="TransactionScopeCallHandlerData"/> containing
		/// the configuration data from this node.
		/// </returns>
		/// <remarks>
		/// Subclasses implement this method to return an instance of a concrete <see cref="T:Microsoft.Practices.EnterpriseLibrary.PolicyInjection.Configuration.CallHandlerData"/> subclass.
		/// </remarks>
		public override CallHandlerData CreateCallHandlerData()
		{
			TransactionScopeCallHandlerData callHandlerData = new TransactionScopeCallHandlerData(Name);
			callHandlerData.TransactionScopeOption = TransactionScopeOption;
			callHandlerData.Timeout = Timeout;
			callHandlerData.IsolationLevel = IsolationLevel;
			callHandlerData.InteropOption = InteropOption;
			callHandlerData.Complete = Complete;

			return callHandlerData;
		}
		#endregion
	}
}
